{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 输入和输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "#函数可以接受多个字符串，用逗号“,”隔开，就可以连成一串输出：遇到逗号“,”会输出一个空格\n",
    "print('hello','world')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "500\n",
      "300\n",
      "100 + 200 =  300\n"
     ]
    }
   ],
   "source": [
    "# 可以打印整数，或者计算结果：\n",
    "print(500)\n",
    "print(100+200)\n",
    "\n",
    "# 可以把计算100 + 200的结果打印得更漂亮一点：\n",
    "print('100 + 200 = ',100+200)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dragon\n"
     ]
    }
   ],
   "source": [
    "# 当你输入name = input()并运行后，Python交互式命令行就在等待你的输入了。这时，你可以输入任意字符，然后按回车后完成输入。\n",
    "name = input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello\n",
      "dragon hello\n"
     ]
    }
   ],
   "source": [
    "name = input()\n",
    "print('dragon',name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的名字china\n",
      "dragon china\n"
     ]
    }
   ],
   "source": [
    "name = input('请输入你的名字')\n",
    "print('dragon',name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 * 10 = 100\n"
     ]
    }
   ],
   "source": [
    "print('10 * 10 =', 10 * 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据类型和变量"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm ok\n"
     ]
    }
   ],
   "source": [
    "print('I\\'m ok')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm learning\n",
      "Python\n"
     ]
    }
   ],
   "source": [
    "# 转义字符\\可以转义很多字符，比如\\n表示换行，\\t表示制表符，字符\\本身也要转义，所以\\\\表示的字符就是\\\n",
    "print('I\\'m learning\\nPython')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\\t\\\n",
      "\\\\\\t\\\\\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串里面有很多字符都需要转义，就需要加很多\\，为了简化，Python还允许用r''表示''内部的字符串默认不转义，试试：\n",
    "print('\\\\\\t\\\\')\n",
    "print(r'\\\\\\t\\\\')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "line1\n",
      "... line2\n",
      "... line3\n"
     ]
    }
   ],
   "source": [
    "# 如果字符串内部有很多换行，用\\n写在一行里不好阅读，为了简化，Python允许用'''...'''的格式表示多行内容\n",
    "print('''line1\n",
    "... line2\n",
    "... line3''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 > 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 > 2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔值可以用and、or和not运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# and运算是与运算，只有所有都为True，and运算结果才是True：\n",
    "True and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 > 2 and 2 > 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# or运算是或运算，只要其中有一个为True，or运算结果就是True：\n",
    "True or False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 > 4 or 2 >1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# not运算是非运算，它是一个单目运算符，把True变成False，False变成True：\n",
    "not True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "not 1 > 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "adult\n"
     ]
    }
   ],
   "source": [
    "age = 20\n",
    "\n",
    "if age >= 18:\n",
    "    print('adult')\n",
    "else:\n",
    "    print('teenager')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "ABC\n"
     ]
    }
   ],
   "source": [
    "# 在Python中，等号=是赋值语句，可以把任意数据类型赋值给变量，同一个变量可以反复赋值，而且可以是不同类型的变量\n",
    "\n",
    "a = 123 # a是整数\n",
    "print(a)\n",
    "a = 'ABC' # a变为字符串\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.3333333333333335"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 所谓常量就是不能变的变量，比如常用的数学常数π就是一个常量。在Python中，通常用全部大写的变量名表示常量：\n",
    "\n",
    "PI = 3.14159265359\n",
    "\n",
    "# 但事实上PI仍然是一个变量，Python根本没有任何机制保证PI不会被改变，所以，用全部大写的变量名表示常量只是一个习惯上的用法，如果你一定要改变变量PI的值，也没人能拦住你。\n",
    "\n",
    "# 最后解释一下整数的除法为什么也是精确的。在Python中，有两种除法，一种除法是/：\n",
    "\n",
    "\n",
    "10 / 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# / 除法计算结果是浮点数，即使是两个整数恰好整除，结果也是浮点数：\n",
    "9 / 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 还有一种除法是//，称为地板除，两个整数的除法仍然是整数：\n",
    "10 // 3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "List（列表） 是 Python 中使用最频繁的数据类型。\n",
    "\n",
    "列表可以完成大多数集合类的数据结构实现。它支持字符，数字，字符串甚至可以包含列表（即嵌套）。\n",
    "\n",
    "列表用 [ ] 标识，是 python 最通用的复合数据类型。\n",
    "\n",
    "列表中值的切割也可以用到变量 [头下标:尾下标] ，就可以截取相应的列表，从左到右索引默认 0 开始，从右到左索引默认 -1 开始，下标可以为空表示取到头或尾。\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['runoob', 786, 2.23, 'john', 70.2]\n",
      "runoob\n",
      "[786, 2.23]\n",
      "[2.23, 'john', 70.2]\n",
      "[123, 'john', 123, 'john']\n",
      "['runoob', 786, 2.23, 'john', 70.2, 123, 'john']\n"
     ]
    }
   ],
   "source": [
    "# 加号 + 是列表连接运算符，星号 * 是重复操作\n",
    "\n",
    "list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]\n",
    "tinylist = [123, 'john']\n",
    "\n",
    "print (list)# 输出完整列表\n",
    "print(list[0])# 输出第一个元素\n",
    "print(list[1:3])# 输出第二个至第三个元素 \n",
    "print(list[2:])# 输出从第三个开始至列表末尾的所有元素\n",
    "print(tinylist * 2)# 输出列表两次\n",
    "print(list + tinylist)# 打印组合的列表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "元组是另一个数据类型，类似于 List（列表）。\n",
    "\n",
    "元组用 () 标识。内部元素用逗号隔开。但是元组不能二次赋值，相当于只读列表。\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('runoob', 786, 2.23, 'john', 70.2)\n",
      "runoob\n",
      "(786, 2.23)\n",
      "(2.23, 'john', 70.2)\n",
      "(123, 'john', 123, 'john')\n",
      "('runoob', 786, 2.23, 'john', 70.2, 123, 'john')\n"
     ]
    }
   ],
   "source": [
    "tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )\n",
    "tinytuple = (123, 'john')\n",
    " \n",
    "print (tuple)               # 输出完整元组\n",
    "print (tuple[0])         # 输出元组的第一个元素\n",
    "print (tuple[1:3])          # 输出第二个至第四个（不包含）的元素 \n",
    "print (tuple[2:])           # 输出从第三个开始至列表末尾的所有元素\n",
    "print (tinytuple * 2)       # 输出元组两次\n",
    "print (tuple + tinytuple)  # 打印组合的元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 以下是元组无效的，因为元组是不允许更新的。而列表是允许更新的：\n",
    "tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )\n",
    "list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]\n",
    "tuple[2] = 1000    # 元组中是非法应用\n",
    "list[2] = 1000     # 列表中是合法应用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象集合，字典是无序的对象集合。\n",
    "\n",
    "两者之间的区别在于：字典当中的元素是通过键来存取的，而不是通过偏移存取。\n",
    "\n",
    "字典用\"{ }\"标识。字典由索引(key)和它对应的值value组成。\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is one\n",
      "This is two\n",
      "{'name': 'john', 'code': 6734, 'dept': 'sales'}\n",
      "dict_keys(['name', 'code', 'dept'])\n",
      "dict_values(['john', 6734, 'sales'])\n"
     ]
    }
   ],
   "source": [
    "dict = {}\n",
    "dict['one'] = \"This is one\"\n",
    "dict[2] = \"This is two\"\n",
    " \n",
    "tinydict = {'name': 'john','code':6734, 'dept': 'sales'}\n",
    " \n",
    " \n",
    "print (dict['one'])          # 输出键为'one' 的值\n",
    "print (dict[2])             # 输出键为 2 的值\n",
    "print (tinydict)             # 输出完整的字典\n",
    "print (tinydict.keys())      # 输出所有键\n",
    "print (tinydict.values())    # 输出所有值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python数据类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有时候，我们需要对数据内置的类型进行转换，数据类型的转换，你只需要将数据类型作为函数名即可\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
